<template>
	<view class="wrap">
		<u-navbar :is-back="false" :title="home.title" title-bold :background="home.background"
			:title-color="home.titleColor">
			<!-- <view class="navbar-right" slot="right">
				<view class="message-box" @click="scanClick" style="padding-right: 30rpx;">
					<u-icon name="scan" size="40" color="#fff"></u-icon>
				</view>
			</view> -->
		</u-navbar>
		<!-- 轮播图 -->
		<uni-swiper-dot class="uni-swiper-dot-box" :info="data" :current="current" field="content">
			<swiper class="swiper-box" :current="swiperDotIndex" @change="changeSwiper">
				<swiper-item v-for="(item, index) in data" :key="index">
					<view class="swiper-item" @click="clickBannerItem(item)">
						<image :src="item.image" mode="aspectFill" :draggable="false" />
					</view>
				</swiper-item>
			</swiper>
		</uni-swiper-dot>

		<view class="toolbar">
			<u-grid class="grid" :col="4" :border="false">
				<!-- <u-grid-item @click="homeNavigateTo('/pages/sys/workOrder/index')">
					<image src="https://zh.xinplz.com:8093/xinpengservice/staticResources/show/1266427697823744000/ic_management_work_order.png"  class="icon" alt="" mode="widthFix"/>
					<view class="grid-text">工单管理</view>
				</u-grid-item>
				<u-grid-item>
					<image src="https://zh.xinplz.com:8093/xinpengservice/staticResources/show/1266427525572067328/ic_work_order_waiting_normal.png"  class="icon" alt="" mode="widthFix"/>
					<view class="grid-text">待进行</view>
				</u-grid-item>
				<u-grid-item @click="homeNavigateTo('/pages/sys/device/index')">
					<image src="https://zh.xinplz.com:8093/xinpengservice/staticResources/show/1266427247380660224/ic_management_equipment.png"  class="icon" alt="" mode="widthFix"/>
					<view class="grid-text">设备管理</view>
				</u-grid-item> -->
				<!-- <u-grid-item @click="homeNavigateTo('/pages/sys/meterReading/index')">
					<image src="static/images/ic_reading_meter.png"  class="icon" alt="" mode="widthFix"/>
					<view class="grid-text">抄表</view>
				</u-grid-item> -->
				<!-- <u-grid-item @click="homeNavigateTo('/pages/sys/maintenance/index')">
					<image src="https://zh.xinplz.com:8093/xinpengservice/staticResources/show/1266426990366294016/ic_maintain_task.png"  class="icon" alt="" mode="widthFix"/>
					<view class="grid-text">维保任务</view>
				</u-grid-item> -->
				<u-grid-item @click="scanVisitorCheckClick()" v-if="visitorCheck">
					<image src="https://zh.xinplz.com:8093/xinpengservice/staticResources/show/1266424244665843712/ic_visitor_check.png" class="icon" alt="" mode="widthFix"/>
					<view class="grid-text">访客核验</view>
				</u-grid-item>
				<u-grid-item @click="homeNavigateTo('/subPages/visitor/sentryBox')" v-if="sentryBoxCheck">
					<image src="https://zh.xinplz.com:8093/xinpengservice/staticResources/show/1270771334426656768/gangting.png" class="icon" alt="" mode="widthFix"/>
					<view class="grid-text">岗亭登录</view>
				</u-grid-item>
				<u-grid-item @click="homeNavigateTo('/subPages/visitor/home')" v-if="visitorInfoApproveCheck">
					<image src="https://zh.xinplz.com:8093/xinpengservice/staticResources/show/1266426714280427520/ic_visitor_home.png" class="icon" alt="" mode="widthFix"/>
					<view class="grid-text">访客管理</view>
				</u-grid-item>
			</u-grid>
		</view>
		<!-- <u-tabs :list="tabList" :is-scroll="false" :current="currentTab" @change="changeTab"></u-tabs>
		<view class="content" v-if="currentTab == 0">
			<view class="content-list">
				<view class="content-box">
					<view class="content-item" @click="goDetail(item)" v-for="item in workOrderList">
						<view class="item-row">
							<view>{{item.title}}
							</view>
							<view class="noSX">
								<text class="gao" :class="['priority'+item.priority]">{{getPriority(item.priority)}}</text>
							</view>
						</view>
						<view class="item-row">
							<text class="item-huise">工单内容：{{item.content}}</text>
							<text class="item-huise">设备：{{item.deviceName}}</text>
						</view>
						<view class="item-row">
							<text class="item-huise">创建时间：{{parseTime(item.createtime,'{y}-{m}-{d} {h}:{i}')}}</text>
							<button @click.stop.prevent="goDeviceDetail(item)" style="margin-right: 0;line-height:2;" type="primary"
								size="mini" plain="true">设备详情</button>
						</view>
					</view>
				</view>
			</view>
		</view> -->
	</view>
</template>
<script>
	export default {
		data() {
			return {
				//头部标题配置
				home: {
					title: "管理后台",
					background: {
						backgroundColor: '#2373FA',
					},
					titleColor: "#ffffff"
				},
				current: 0,
				swiperDotIndex: 0,
				data: [{
					image: 'https://zh.xinplz.com:8093/xinpengservice/staticResources/show/1266428123981807616/banner04.jpg'
				}],
				currentTab: 0,
				tabList: [{
					name: '工单'
				}, {
					name: '维保',
				}],
				params: {
					pageNum: 1,
					pageSize: 10,
				},
				workOrderList: [],
				list: [{
					name: '已受理',
					value: 0
				}, {
					name: '已分配',
					value: 1
				}, {
					name: '已确认',
					value: 2
				}, {
					name: '已评价',
					value: 3
				}],
				// 优先级数据源
				priorityList: [{
					text: '一般',
					value: 1
				}, {
					text: '紧急',
					value: 2
				}],
				user: {},
				// 访客核验
				visitorCheck: false,
				// 岗亭登录
				sentryBoxCheck: false,
				// 访客管理
				visitorInfoApproveCheck: false,
				
			};
		},
		onLoad(data) {},
		onReady() {},
		onShow() {
			this.checkUser()
			this.getWorkOrderData()
		},
		methods: {
			checkUser(){
				this.visitorCheck = false;
				this.sentryBoxCheck = false;
				this.visitorInfoApproveCheck = false;
				this.user = this.$store.state.vuex_user;
				console.log('user',this.user);
				if(this.user.admin){
					this.visitorCheck = true;
					this.sentryBoxCheck = true;
					this.visitorInfoApproveCheck = true;
				}else {
					this.$u.api.getInfo().then(res => {
						let permissions = res.permissions;
						for (var i = 0; i < permissions.length; i++) {
							let permission = permissions[i];
							if(permission == 'visitor:visitorInfo:query'){
								this.visitorCheck = true;
							}
							if(permission == 'visitor:sentryBox:login'){
								this.sentryBoxCheck = true;
							}
							if(permission == 'visitor:visitorInfo:list'){
								this.visitorInfoApproveCheck = true;
							}
						}
					})
				}
				
			},
			clickBannerItem(item) {
				console.info(item)
			},
			changeSwiper(e) {
				this.current = e.detail.current
			},
			changeGrid(e) {
				this.$modal.showToast('模块建设中~')
			},
			changeTab(index) {
				this.currentTab = index;
			},
			homeNavigateTo(url) {
				uni.navigateTo({
					url: url
				})
			},
			getWorkOrderData() {
				const params = {
					pageNum: this.params.pageNum,
					pageSize: this.params.pageSize,
				}
				this.$u.api.workOrder.getlotFinish0rders(this.params).then(res => {
					this.workOrderList = res.list
				})
			},
			goDeviceDetail(item) {
				uni.navigateTo({
					url: "/pages/sys/device/deviceDetail?deviceId=" + item.deviceId
				})
			},
			goDetail(item) {
				uni.navigateTo({
					url: "/pages/sys/workOrder/detail?id=" + item.id
				})
			},
			getPriority(value) {
				return this.priorityList.find(d => d.value == value)?.text
			},
			//扫码
			scanClick() {
				this.$scanCode().then(res => {
					try {
						const result = JSON.parse(res.resp_result)
						if (result.dataType && result.dataType == 1) {
							setTimeout(() => {
								uni.navigateTo({
									url: `/pages/sys/device/deviceDetail?deviceCode=${result.id}`
								})
							}, 300)
						} else {
							uni.showToast({
								title: `扫码错误${typeof result}`,
								icon: 'error',
							})
						}
					} catch (e) {
						uni.showToast({
							title: `扫码错误`,
							icon: 'error',
						})
					}
				}, err => {})
			},
			
			//扫码
			scanVisitorCheckClick() {
				// #ifdef MP-WEIXIN
					uni.scanCode({
						success:  (res)=> {
							const obj = JSON.parse(res.result)
							console.log(obj);
							if(obj.dataType==2){
								this.$u.api.visitor.getVisitorInfo(obj.id).then(async res => {
									if (res.data){
										setTimeout(() => {
											uni.navigateTo({
												url: `/subPages/visitor/visitorInfoDetail?visitorInfoId=${obj.id}&saoma=1`
											})
										}, 300)
									}else{
										uni.showToast({
											title: `记录不存在`,
											icon: 'error',
										})
									}
								})
							}else{
								this.$u.toast('二维码类型不正确')
							}
							
						}
					})
				// #endif
			},
			//访客验证扫码
			scanVisitorCheckClick2() {
				this.$scanCode().then(res => {
					try {
						const result = JSON.parse(res.resp_result)
						if (result.dataType && result.dataType == 2) {
							this.$u.api.visitor.getVisitorInfo(result.id).then(async res => {
								if (res.data){
									setTimeout(() => {
										uni.navigateTo({
											url: `/pages/sys/visitor/visitorInfoDetail?visitorInfoId=${result.id}`
										})
									}, 300)
								}else{
									uni.showToast({
										title: `记录不存在`,
										icon: 'error',
									})
								}
							})
						} else {
							uni.showToast({
								title: `扫码错误${typeof result}`,
								icon: 'error',
							})
						}
					} catch (e) {
						uni.showToast({
							title: `扫码错误`,
							icon: 'error',
						})
					}
				}, err => {})
			},
			
			//扫码上岗扫码
			scanClick1() {
				this.$scanCode().then(res => {
					try {
						const result = JSON.parse(res.resp_result)
						if (result.dataType && result.dataType == 3) {
							let workParam = {
								'employeeId': 1,
								'workPostId': JSON.parse(result.text).postId,
								'workstationId': JSON.parse(result.text).workstationId,
								'workType': 1
							};
							console.log('workParam:',workParam)
							
							this.$u.api.xinpeng.work(workParam).then(async resWork => {
								if (resWork.data) {
									if(resWork.code == 200){
										
									}else {
										uni.showToast({
											title: resWork.data.msg,
											icon: 'error',
										})
									}
								}else {
									uni.showToast({
										title: resWork.msg,
										icon: 'error',
									})
								}
							})
						} else {
							uni.showToast({
								title: `扫码错误${typeof result}`,
								icon: 'error',
							})
						}
					} catch (e) {
						uni.showToast({
							title: `扫码错误`,
							icon: 'error',
						})
					}
				}, err => {})
			},
			
			//扫码上岗扫码
			scanClick2() {
				this.$scanCode().then(res => {
					try {
						const result = JSON.parse(res.resp_result)
						if (result.dataType && result.dataType == 3) {
							let workParam = {
								'employeeId': 1,
								'workPostId': JSON.parse(result.text).postId,
								'workstationId': JSON.parse(result.text).workstationId,
								'workType': 2
							};
							console.log('workParam:',workParam)
							
							this.$u.api.xinpeng.work(workParam).then(async resWork => {
								if (resWork.data) {
									if(resWork.code == 200){
										
									}else {
										uni.showToast({
											title: resWork.data.msg,
											icon: 'error',
										})
									}
								}else {
									uni.showToast({
										title: resWork.msg,
										icon: 'error',
									})
								}
							})
						} else {
							uni.showToast({
								title: `扫码错误${typeof result}`,
								icon: 'error',
							})
						}
					} catch (e) {
						uni.showToast({
							title: `扫码错误`,
							icon: 'error',
						})
					}
				}, err => {})
			},
		}
	};
</script>
<style lang="scss">
	@import 'index.scss';

	page {
		background: #F7F7F7;
	}

	.toolbar {
		background-color: #fff;
		margin-bottom: 10px;
		padding-top: 20px;
	}

	.wrap .box .item {
		margin: 0 0 0px;
		border-radius: 0;

		.title {
			padding-left: 0;

			.text {
				font-size: 30rpx;
				font-weight: 500;
				margin: 0;
				color: #202328;
			}
		}
	}

	.grid {
		.grid-icon {
			color: #666;
		}
		
		.icon {
			width: 100rpx;
			border-radius: 10px;
		}

		.grid-text {
			height: 75upx;
			text-align: center;
		}

		.grid-text {
			font-size: 25rpx;
			color: #5B5B5B;
			padding: 15rpx;
		}
	}

	.workbench-title {
		font-size: 28rpx;
		font-weight: bold;
		color: #5B5B5B;
		padding: 10px 30rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;

		>view {
			flex: 1;
		}
	}

	.top-box {
		position: relative;
		font-size: 28rpx;
		font-weight: 500;
		font-weight: bold;
		padding: 10px;
		padding-top: 15px;
		display: flex;
		justify-content: space-between;
		margin: 10px;
		border-radius: 7rpx;
		background-color: #2373FA;
		color: #FFFFFF;
		min-height: 100rpx;

		>view {
			flex: 1;
		}

		.item-box {
			display: flex;
			flex-wrap: wrap;

			.item-list {
				overflow: hidden;
				text-overflow: ellipsis;
				flex-basis: 50%;
			}
		}

		.qiehuan {
			position: absolute;
			right: 0;
			top: 10px;
			flex: none;
			width: 83rpx;
			height: 54rpx;
			background: linear-gradient(360deg, #F3F3F3 0%, #EEEEEE 100%);
			box-shadow: 0rpx 4rpx 7rpx 0rpx #065AEA;
			border-radius: 168rpx 0rpx 0rpx 168rpx;
			border: 2rpx solid;
			text-align: center;
			line-height: 60rpx;
		}

	}

	.home-icon i.icon-tongzhi {
		font-size: 22px;
	}

	.swiper {
		height: 300rpx;
	}

	.swiper-box {
		height: 150px;
	}

	.swiper-item {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
		justify-content: center;
		align-items: center;
		color: #fff;
		height: 300rpx;
		line-height: 300rpx;

		>uni-image {
			width: 100%;
		}
	}



	@media screen and (min-width: 500px) {
		.uni-swiper-dot-box {
			width: 400px;
			/* #ifndef APP-NVUE */
			margin: 0 auto;
			/* #endif */
			margin-top: 8px;
		}

		.image {
			width: 100%;
		}
	}

	.content {
		.content-list {

			.content-header {
				display: flex;
				justify-content: space-between;
				padding: 15upx;
				font-size: 35upx;
				font-weight: bold;
			}

			.content-box {
				padding: 0 15upx 15upx 15upx;
				font-size: 30upx;
				font-weight: bold;

				.content-item {
					background-color: #fff;
					border-radius: 5px;
					padding: 10px;
					position: relative;
					border-top: 1px solid #f2f4f8;
					margin-top: 15upx;
					display: flex;
					flex-direction: column;
					box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);

					>text {
						position: absolute;
						right: -15upx;
						top: 0;
						background-color: #B9E1FB;
						color: #2B96C6;
						font-size: 25upx;
						padding: 5upx 10upx;
						border-bottom-left-radius: 5px;
					}

					.item-row {
						display: flex;
						justify-content: space-between;
						height: 35px;
						align-items: center;

						&:first-child {
							border-bottom: 1px solid #f2f4f8;
						}

						.item-huise {
							font-size: 27upx;
							color: #858585;
						}

						.noSX {
							flex-shrink: 0;
						}

						/deep/.u-progress {
							flex: 1;
							height: 8px !important;
							margin: 0 10px;
						}

						.gao {
							background-color: #ffaa00;
							border-radius: 10rpx;
							padding: 0 10rpx;
							display: inline-block;
							color: #fff;
							font-size: 25upx;
							line-height: 40upx;
							font-weight: 100;
							text-align: center;
							margin-right: 10upx;
						}

						.priority1 {
							background-color: #4CB2DB;
						}

						.priority2 {
							background-color: #fa3534;
						}

						/deep/.u-tag {
							margin-left: 10upx;
							padding: 4px;
							font-size: 11px;
						}
					}
				}
			}
		}
	}
</style>